package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.IpAddressVersionAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname IpAddressVersionAttrLinkHandler
 * @Description 客户自带IP地址版本
 * @Author zhang.song
 * @Date 2022-03-10 19:07
 */
@Service
@AttrHandler(IpAddressVersionAttrHandler.class)
public class IpAddressVersionAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {


    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if(Objects.isNull(attr) || KiteStringUtils.isEmpty(attr.getValue()) || KiteStringUtils.isEqual(attr.getShowType(), KeyConsts.PROD_ATTR_SHOW_TYPE_E)) {
            return;
        }

        /*
         * 客户自带IP地址版本（IPv4：1_0000219；IPv6：1_0000220；IPv4/IPv6双栈：1_0000221）
         *
         * 1.客户自带IP地址版本=IPv4，显示：
         *    210008284  IPv4穿透模式
         *    210008283  IPv4路由协议类型
         *    210008159  客户自带广播IPv4地址段
         *    200001556  IPv4互联地址客户侧
         *    200001557  IPv4互联地址联通侧
         *
         * 2.客户自带IP地址版本=IPv6，显示：
         *    210008200  IPv6穿透模式
         *    210008163  IPv6路由协议类型
         *    210008160  客户自带广播IPv6地址段
         *    210008161  IPv6互联地址客户侧
         *    210008162  IPv6互联地址联通侧
         *
         * 3.客户自带IP地址版本=IPv4/IPv6双栈，以上全显示
         */

        // IPv4路由协议类型、IPv6路由协议类型  联动处理类
        KiteAttr ip4RouteAttr = inst.getAttr("210008283");
        KiteAttr ip6RouteAttr = inst.getAttr("210008163");
        RouteProtocolTypeAttrLinkHandler routeProtocolTypeAttrLinkHandler = SpringUtil.getBean(RouteProtocolTypeAttrLinkHandler.class);

        if(KiteStringUtils.isEqual(attr.getValue(), "1_0000219")) {
            // 客户自带IP地址版本=IPv4
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "210008284","210008283","210008159","200001556","200001557");
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "210008200","210008163","210008160","210008161","210008162");

            if (Objects.nonNull(ip4RouteAttr)) {
                routeProtocolTypeAttrLinkHandler.handle(ip4RouteAttr, inst, data);
            }
        } else if(KiteStringUtils.isEqual(attr.getValue(), "1_0000220")) {
            // 客户自带IP地址版本=IPv6
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "210008284","210008283","210008159","200001556","200001557");
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "210008200","210008163","210008160","210008161","210008162");

            if (Objects.nonNull(ip6RouteAttr)) {
                routeProtocolTypeAttrLinkHandler.handle(ip6RouteAttr, inst, data);
            }
        } else if(KiteStringUtils.isEqual(attr.getValue(), "1_0000221")) {
            // 客户自带IP地址版本=IPv4/IPv6双栈
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "210008284","210008283","210008159","200001556","200001557");
            LocalAttrHelper.setShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "210008200","210008163","210008160","210008161","210008162");

            if (Objects.nonNull(ip4RouteAttr)) {
                routeProtocolTypeAttrLinkHandler.handle(ip4RouteAttr, inst, data);
            }
            if (Objects.nonNull(ip6RouteAttr)) {
                routeProtocolTypeAttrLinkHandler.handle(ip6RouteAttr, inst, data);
            }
        }

        // 需要处理  IP地址业务变更场景（处理逻辑与  联通分配IP地址版本  属性共用）
        /*KiteAttr ipAddressVersionAttr1 = inst.getAttr("2_0000047");
        LocalIPProtocolVerAttrLinkHandler localIPProtocolVerAttrLinkHandler = SpringUtil.getBean(LocalIPProtocolVerAttrLinkHandler.class);
        localIPProtocolVerAttrLinkHandler.modifyOperateAttrHandle(ipAddressVersionAttr1, inst, data);*/
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
